#!/bin/sh

# Slackware build script for fglrx-driver

# Copyright (c) 2008, Antonio Hern�ndez Blas <hba.nihilismus@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1.- Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This script is just a binary repackaging.

PRGNAM=fglrx-driver
VERSION=8.512
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SRCNAM=ati-driver-installer-8-7-x86.x86_64.run
ATI_PKG=fglrx-x710-$VERSION-x86-1.tgz

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $PKG
sh $CWD/$SRCNAM --buildpkg Slackware/Only_X || exit 1
explodepkg $PKG/$ATI_PKG
rm $PKG/$ATI_PKG
chown -R root:root .

# Move manpages and docs to their right place
mv $PKG/usr/share/doc $PKG/usr
mv $PKG/usr/doc/fglrx $PKG/usr/doc/$PRGNAM-$VERSION

# This is the only file that exist in the system, so let's rename it.
# Notes:
#   Once you have installed this package, use 'fglrx-switch -fglrx' 
#     to set the correct libGL.so.1.2 for the fglrx driver.
#   Before you uninstall or upgrade this package, make use of 'fglrx-switch -mesa'
#     to return libs/symlinks as were.
mv $PKG/usr/lib/libGL.so.1.2 $PKG/usr/lib/libGL.so.1.2-fglrx-driver

# Add 'fglrx-switch'
install -D -m 0755 $CWD/fglrx-switch $PKG/usr/sbin/fglrx-switch

# Add 'kdesu' to the Exec line in the KDE desktop file, so you
# can run de 'ATI Catalyst Control Center' as root in a different
# X session user.
sed -i 's/^Exec=amdcccle$/Exec=kdesu -i ccc_large.xpm amdcccle/' \
  $PKG/usr/share/applnk/amdcccle.kdelnk
# Do the same, but with 'gksu', in GNOME desktop file
sed -i 's/^Exec=amdcccle$/Exec=gksu amdcccle/' \
  $PKG/usr/share/gnome/apps/amdcccle.desktop

# There's some fglrx sample code under $PKG/usr/src/ati, so lets
# move it into docs dir
mv $PKG/usr/src/ati $PKG/usr/doc/$PRGNAM-$VERSION
rmdir $PKG/usr/src
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README.SBo > $PKG/usr/doc/$PRGNAM-$VERSION/README.SBo

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
find $PKG/install -type f -exec chmod 644 {} \;

# Fix permissions; there are some executable files and libs with mode 0744
find $PKG/usr -type f -perm 744 -exec chmod 755 {} \;

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
